package com.yy.micro.magic.data.model.converter;

import com.yy.micro.magic.data.model.utils.JsonUtils;

import java.util.LinkedHashSet;
import java.util.Set;

import javax.persistence.AttributeConverter;

public class TypedSetConverter implements AttributeConverter<Set<Long>,String> {
    @Override
    public String convertToDatabaseColumn(Set<Long> attribute) {
        if (null == attribute ||attribute.isEmpty()){
            return null;
        }
        return JsonUtils.toJson(attribute);
    }

    @Override
    public Set<Long> convertToEntityAttribute(String dbData) {
        if (null == dbData ||dbData.trim().length() <1){
            return null;
        }
        return (Set) JsonUtils.fromJsonAsCollection(dbData, LinkedHashSet.class,Long.class);
    }
}
